home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / qe4 / win_qe3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  12.4 KB  |  584 lines

  1. #include "qe3.h"
  2. #include "mru.h"
  3.  
  4. int    screen_width;
  5. int    screen_height;
  6. qboolean    have_quit;
  7.  
  8. int    update_bits;
  9.  
  10. HANDLE    bsp_process;
  11.  
  12. //===========================================
  13.  
  14. void Sys_SetTitle (char *text)
  15. {
  16.     SetWindowText (g_qeglobals.d_hwndMain, text);
  17. }
  18.  
  19. HCURSOR    waitcursor;
  20.  
  21. void Sys_BeginWait (void)
  22. {
  23.     waitcursor = SetCursor (LoadCursor (NULL, IDC_WAIT));
  24. }
  25.  
  26. void Sys_EndWait (void)
  27. {
  28.     if (waitcursor)
  29.     {
  30.         SetCursor (waitcursor);
  31.         waitcursor = NULL;
  32.     }
  33. }
  34.  
  35.  
  36. void Sys_GetCursorPos (int *x, int *y)
  37. {
  38.     POINT lpPoint;
  39.  
  40.     GetCursorPos (&lpPoint);
  41.     *x = lpPoint.x;
  42.     *y = lpPoint.y;
  43. }
  44.  
  45. void Sys_SetCursorPos (int x, int y)
  46. {
  47.     SetCursorPos (x, y);
  48. }
  49.  
  50. void Sys_UpdateWindows (int bits)
  51. {
  52. //    Sys_Printf("updating 0x%X\n", bits);
  53.     update_bits |= bits;
  54. //update_bits = -1;
  55. }
  56.  
  57.  
  58. void Sys_Beep (void)
  59. {
  60.     MessageBeep (MB_ICONASTERISK);
  61. }
  62.  
  63. char    *TranslateString (char *buf)
  64. {
  65.     static    char    buf2[32768];
  66.     int        i, l;
  67.     char    *out;
  68.  
  69.     l = strlen(buf);
  70.     out = buf2;
  71.     for (i=0 ; i<l ; i++)
  72.     {
  73.         if (buf[i] == '\n')
  74.         {
  75.             *out++ = '\r';
  76.             *out++ = '\n';
  77.         }
  78.         else
  79.             *out++ = buf[i];
  80.     }
  81.     *out++ = 0;
  82.  
  83.     return buf2;
  84. }
  85.  
  86. void Sys_ClearPrintf (void)
  87. {
  88.     char    text[4];
  89.  
  90.     text[0] = 0;
  91.  
  92.     SendMessage (g_qeglobals.d_hwndEdit,
  93.         WM_SETTEXT,
  94.         0,
  95.         (LPARAM)text);
  96. }
  97.  
  98. void Sys_Printf (char *text, ...)
  99. {
  100.     va_list argptr;
  101.     char    buf[32768];
  102.     char    *out;
  103.  
  104.     va_start (argptr,text);
  105.     vsprintf (buf, text,argptr);
  106.     va_end (argptr);
  107.  
  108.     out = TranslateString (buf);
  109.  
  110. #ifdef LATER
  111.     Sys_Status(out);
  112. #else
  113.     SendMessage (g_qeglobals.d_hwndEdit,
  114.         EM_REPLACESEL,
  115.         0,
  116.         (LPARAM)out);
  117. #endif
  118.  
  119. }
  120.  
  121. double Sys_DoubleTime (void)
  122. {
  123.     return clock()/ 1000.0;
  124. }
  125.  
  126. void PrintPixels (HDC hDC)
  127. {
  128.     int        i;
  129.     PIXELFORMATDESCRIPTOR p[64];
  130.  
  131.     printf ("### flags color layer\n");
  132.     for (i=1 ; i<64 ; i++)
  133.     {
  134.         if (!DescribePixelFormat ( hDC, i, sizeof(p[0]), &p[i]))
  135.             break;
  136.         printf ("%3i %5i %5i %5i\n", i,
  137.             p[i].dwFlags,
  138.             p[i].cColorBits,
  139.             p[i].bReserved);
  140.     }
  141.     printf ("%i modes\n", i-1);
  142. }
  143.  
  144.  
  145.  
  146. //==========================================================================
  147.  
  148. void QEW_StopGL( HWND hWnd, HGLRC hGLRC, HDC hDC )
  149. {
  150.     wglMakeCurrent( NULL, NULL );
  151.     wglDeleteContext( hGLRC );
  152.     ReleaseDC( hWnd, hDC );
  153. }
  154.         
  155. int QEW_SetupPixelFormat(HDC hDC, qboolean zbuffer )
  156. {
  157.     static PIXELFORMATDESCRIPTOR pfd = {
  158.         sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd
  159.         1,                                // version number
  160.         PFD_DRAW_TO_WINDOW |            // support window
  161.         PFD_SUPPORT_OPENGL |            // support OpenGL
  162.         PFD_DOUBLEBUFFER,                // double buffered
  163.         PFD_TYPE_RGBA,                    // RGBA type
  164.         24,                                // 24-bit color depth
  165.         0, 0, 0, 0, 0, 0,                // color bits ignored
  166.         0,                                // no alpha buffer
  167.         0,                                // shift bit ignored
  168.         0,                                // no accumulation buffer
  169.         0, 0, 0, 0,                        // accum bits ignored
  170.         32,                                // depth bits
  171.         0,                                // no stencil buffer
  172.         0,                                // no auxiliary buffer
  173.         PFD_MAIN_PLANE,                    // main layer
  174.         0,                                // reserved
  175.         0, 0, 0                            // layer masks ignored
  176.     };
  177.     int pixelformat = 0;
  178.  
  179.     zbuffer = true;
  180.     if ( !zbuffer )
  181.         pfd.cDepthBits = 0;
  182.  
  183.     if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
  184.     {
  185.         printf("%d",GetLastError());
  186.         Error ("ChoosePixelFormat failed");
  187.     }
  188.  
  189.     if (!SetPixelFormat(hDC, pixelformat, &pfd))
  190.         Error ("SetPixelFormat failed");
  191.  
  192.     return pixelformat;
  193. }
  194.  
  195. /*
  196. =================
  197. Error
  198.  
  199. For abnormal program terminations
  200. =================
  201. */
  202. void Error (char *error, ...)
  203. {
  204.     va_list argptr;
  205.     char    text[1024];
  206.     char    text2[1024];
  207.     int        err;
  208.  
  209.     err = GetLastError ();
  210.  
  211.     va_start (argptr,error);
  212.     vsprintf (text, error,argptr);
  213.     va_end (argptr);
  214.  
  215.     sprintf (text2, "%s\nGetLastError() = %i", text, err);
  216.     MessageBox(g_qeglobals.d_hwndMain, text2, "Error", 0 /* MB_OK */ );
  217.  
  218.     exit (1);
  219. }
  220.  
  221. /*
  222. ======================================================================
  223.  
  224. FILE DIALOGS
  225.  
  226. ======================================================================
  227. */
  228.  
  229. qboolean ConfirmModified (void)
  230. {
  231.     if (!modified)
  232.         return true;
  233.  
  234.     if (MessageBox (g_qeglobals.d_hwndMain, "This will lose changes to the map"
  235.         , "warning", MB_OKCANCEL) == IDCANCEL)
  236.         return false;
  237.     return true;
  238. }
  239.  
  240. static OPENFILENAME ofn;       /* common dialog box structure   */ 
  241. static char szDirName[MAX_PATH];    /* directory string              */ 
  242. static char szFile[260];       /* filename string               */ 
  243. static char szFileTitle[260];  /* file title string             */ 
  244. static char szFilter[260] =     /* filter string                 */ 
  245.     "QuakeEd file (*.map)\0*.map\0\0";
  246. static char szProjectFilter[260] =     /* filter string                 */ 
  247.     "QuakeEd project (*.qe4)\0*.qe4\0\0";
  248. static char chReplace;         /* string separator for szFilter */ 
  249. static int i, cbString;        /* integer count variables       */ 
  250. static HANDLE hf;              /* file handle                   */ 
  251.  
  252. void OpenDialog (void)
  253. {
  254.     /* 
  255.      * Obtain the system directory name and 
  256.      * store it in szDirName. 
  257.      */ 
  258.  
  259.     strcpy (szDirName, ValueForKey (g_qeglobals.d_project_entity, "basepath") );
  260.     strcat (szDirName, "\\maps");
  261.  
  262.     /* Place the terminating null character in the szFile. */ 
  263.  
  264.     szFile[0] = '\0'; 
  265.  
  266.     /* Set the members of the OPENFILENAME structure. */ 
  267.  
  268.     ofn.lStructSize = sizeof(OPENFILENAME); 
  269.     ofn.hwndOwner = g_qeglobals.d_hwndCamera;
  270.     ofn.lpstrFilter = szFilter; 
  271.     ofn.nFilterIndex = 1; 
  272.     ofn.lpstrFile = szFile; 
  273.     ofn.nMaxFile = sizeof(szFile); 
  274.     ofn.lpstrFileTitle = szFileTitle; 
  275.     ofn.nMaxFileTitle = sizeof(szFileTitle); 
  276.     ofn.lpstrInitialDir = szDirName; 
  277.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | 
  278.         OFN_FILEMUSTEXIST; 
  279.  
  280.     /* Display the Open dialog box. */ 
  281.  
  282.     if (!GetOpenFileName(&ofn))
  283.         return;    // canceled
  284.  
  285.     // Add the file in MRU.
  286.     AddNewItem( g_qeglobals.d_lpMruMenu, ofn.lpstrFile);
  287.  
  288.     // Refresh the File menu.
  289.     PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0), 
  290.             ID_FILE_EXIT);
  291.  
  292.     /* Open the file. */ 
  293.  
  294.     Map_LoadFile (ofn.lpstrFile);    
  295. }
  296.  
  297. void ProjectDialog (void)
  298. {
  299.     /* 
  300.      * Obtain the system directory name and 
  301.      * store it in szDirName. 
  302.      */ 
  303.  
  304.     strcpy (szDirName, ValueForKey(g_qeglobals.d_project_entity, "basepath") );
  305.     strcat (szDirName, "\\scripts");
  306.  
  307.     /* Place the terminating null character in the szFile. */ 
  308.  
  309.     szFile[0] = '\0'; 
  310.  
  311.     /* Set the members of the OPENFILENAME structure. */ 
  312.  
  313.     ofn.lStructSize = sizeof(OPENFILENAME); 
  314.     ofn.hwndOwner = g_qeglobals.d_hwndCamera;
  315.     ofn.lpstrFilter = szProjectFilter; 
  316.     ofn.nFilterIndex = 1; 
  317.     ofn.lpstrFile = szFile; 
  318.     ofn.nMaxFile = sizeof(szFile); 
  319.     ofn.lpstrFileTitle = szFileTitle; 
  320.     ofn.nMaxFileTitle = sizeof(szFileTitle); 
  321.     ofn.lpstrInitialDir = szDirName; 
  322.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | 
  323.         OFN_FILEMUSTEXIST; 
  324.  
  325.     /* Display the Open dialog box. */ 
  326.  
  327.     if (!GetOpenFileName(&ofn))
  328.         return;    // canceled
  329.  
  330.     // Refresh the File menu.
  331.     PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0), 
  332.             ID_FILE_EXIT);
  333.  
  334.     /* Open the file. */ 
  335.     if (!QE_LoadProject(ofn.lpstrFile))
  336.         Error ("Couldn't load project file");
  337. }
  338.  
  339.  
  340. void SaveAsDialog (void)
  341.     strcpy (szDirName, ValueForKey (g_qeglobals.d_project_entity, "basepath") );
  342.     strcat (szDirName, "\\maps");
  343.  
  344.     /* Place the terminating null character in the szFile. */ 
  345.  
  346.     szFile[0] = '\0'; 
  347.  
  348.     /* Set the members of the OPENFILENAME structure. */ 
  349.  
  350.     ofn.lStructSize = sizeof(OPENFILENAME); 
  351.     ofn.hwndOwner = g_qeglobals.d_hwndCamera;
  352.     ofn.lpstrFilter = szFilter; 
  353.     ofn.nFilterIndex = 1; 
  354.     ofn.lpstrFile = szFile; 
  355.     ofn.nMaxFile = sizeof(szFile); 
  356.     ofn.lpstrFileTitle = szFileTitle; 
  357.     ofn.nMaxFileTitle = sizeof(szFileTitle); 
  358.     ofn.lpstrInitialDir = szDirName; 
  359.     ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | 
  360.         OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT; 
  361.  
  362.     /* Display the Open dialog box. */ 
  363.  
  364.     if (!GetSaveFileName(&ofn))
  365.         return;    // canceled
  366.   
  367.     DefaultExtension (ofn.lpstrFile, ".map");
  368.     strcpy (currentmap, ofn.lpstrFile);
  369.  
  370.     // Add the file in MRU.
  371.     AddNewItem(g_qeglobals.d_lpMruMenu, ofn.lpstrFile);
  372.  
  373.     // Refresh the File menu.
  374.     PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0),
  375.             ID_FILE_EXIT);
  376.  
  377.     Map_SaveFile (ofn.lpstrFile, false);    // ignore region
  378. }
  379.  
  380. /*
  381. =======================================================
  382.  
  383. Menu modifications
  384.  
  385. =======================================================
  386. */
  387.  
  388. /*
  389. ==================
  390. FillBSPMenu
  391.  
  392. ==================
  393. */
  394. char    *bsp_commands[256];
  395.  
  396. void FillBSPMenu (void)
  397. {
  398.     HMENU    hmenu;
  399.     epair_t    *ep;
  400.     int        i;
  401.     static int count;
  402.  
  403.     hmenu = GetSubMenu (GetMenu(g_qeglobals.d_hwndMain), MENU_BSP);
  404.  
  405.     for (i=0 ; i<count ; i++)
  406.         DeleteMenu (hmenu, CMD_BSPCOMMAND+i, MF_BYCOMMAND);
  407.     count = 0;
  408.  
  409.     i = 0;
  410.     for (ep = g_qeglobals.d_project_entity->epairs ; ep ; ep=ep->next)
  411.     {
  412.         if (ep->key[0] == 'b' && ep->key[1] == 's' && ep->key[2] == 'p')
  413.         {
  414.             bsp_commands[i] = ep->key;
  415.             AppendMenu (hmenu, MF_ENABLED|MF_STRING,
  416.             CMD_BSPCOMMAND+i, (LPCTSTR)ep->key);
  417.             i++;
  418.         }
  419.     }
  420.     count = i;
  421. }
  422.  
  423. //==============================================
  424.  
  425. /*
  426. ===============
  427. CheckBspProcess
  428.  
  429. See if the BSP is done yet
  430. ===============
  431. */
  432. void CheckBspProcess (void)
  433. {
  434.     char    outputpath[1024];
  435.     char    temppath[512];
  436.     DWORD    exitcode;
  437.     char    *out;
  438.     BOOL    ret;
  439.  
  440.     if (!bsp_process)
  441.         return;
  442.  
  443.     ret = GetExitCodeProcess (bsp_process, &exitcode);
  444.     if (!ret)
  445.         Error ("GetExitCodeProcess failed");
  446.     if (exitcode == STILL_ACTIVE)
  447.         return;
  448.  
  449.     bsp_process = 0;
  450.  
  451.     GetTempPath(512, temppath);
  452.     sprintf (outputpath, "%sjunk.txt", temppath);
  453.  
  454.     LoadFile (outputpath, (void *)&out);
  455.     Sys_Printf ("%s", out);
  456.     Sys_Printf ("\ncompleted.\n");
  457.     free (out);
  458.     Sys_Beep ();
  459.  
  460.     Pointfile_Check ();
  461. }
  462.  
  463. extern int    cambuttonstate;
  464.  
  465. /*
  466. ==================
  467. WinMain
  468.  
  469. ==================
  470. */
  471. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance
  472.                     ,LPSTR lpCmdLine, int nCmdShow)
  473. {
  474.     MSG        msg;
  475.     double        time, oldtime, delta;
  476.     HACCEL        accelerators;
  477.  
  478.     g_qeglobals.d_hInstance = hInstance;
  479.  
  480.     InitCommonControls ();
  481.  
  482.     screen_width = GetSystemMetrics (SM_CXFULLSCREEN);
  483.     screen_height = GetSystemMetrics (SM_CYFULLSCREEN);
  484.  
  485.     // hack for broken NT 4.0 dual screen
  486.     if (screen_width > 2*screen_height)
  487.         screen_width /= 2;
  488.  
  489.     accelerators = LoadAccelerators (hInstance
  490.         , MAKEINTRESOURCE(IDR_ACCELERATOR1));
  491.     if (!accelerators)
  492.         Error ("LoadAccelerators failed");
  493.  
  494.     Main_Create (hInstance);
  495.  
  496.     WCam_Create (hInstance);
  497.     WXY_Create (hInstance);
  498.     WZ_Create (hInstance);
  499.     CreateEntityWindow(hInstance);
  500.  
  501.     // the project file can be specified on the command line,
  502.     // or implicitly found in the scripts directory
  503.     if (lpCmdLine && strlen(lpCmdLine))
  504.     {
  505.         ParseCommandLine (lpCmdLine);
  506.         if (!QE_LoadProject(argv[1]))
  507.             Error ("Couldn't load %s project file", argv[1]);
  508.     }
  509.     else if (!QE_LoadProject("scripts/quake.qe4"))
  510.         Error ("Couldn't load scripts/quake.qe4 project file");
  511.  
  512.     QE_Init ();
  513.  
  514.     Sys_Printf ("Entering message loop\n");
  515.  
  516.     oldtime = Sys_DoubleTime ();
  517.  
  518.     while (!have_quit)
  519.     {
  520.         Sys_EndWait ();        // remove wait cursor if active
  521.  
  522.         while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  523.         {
  524.             if (!TranslateAccelerator(g_qeglobals.d_hwndMain, accelerators, &msg) )
  525.             {
  526.                   TranslateMessage (&msg);
  527.                   DispatchMessage (&msg);
  528.             }
  529.             if (msg.message == WM_QUIT)
  530.                 have_quit = true;
  531.         }
  532.  
  533.  
  534.         CheckBspProcess ();
  535.  
  536.         time = Sys_DoubleTime ();
  537.         delta = time - oldtime;
  538.         oldtime = time;
  539.         if (delta > 0.2)
  540.             delta = 0.2;
  541.  
  542.         // run time dependant behavior
  543.         Cam_MouseControl (delta);
  544.  
  545.         // update any windows now
  546.         if (update_bits & W_CAMERA)
  547.         {
  548.             InvalidateRect(g_qeglobals.d_hwndCamera, NULL, false);
  549.             UpdateWindow (g_qeglobals.d_hwndCamera);
  550.         }
  551.         if (update_bits & (W_Z | W_Z_OVERLAY) )
  552.         {
  553.             InvalidateRect(g_qeglobals.d_hwndZ, NULL, false);
  554.             UpdateWindow (g_qeglobals.d_hwndZ);
  555.         }
  556.             
  557.         if ( update_bits & W_TEXTURE )
  558.         {
  559.             InvalidateRect(g_qeglobals.d_hwndTexture, NULL, false);
  560.             UpdateWindow (g_qeglobals.d_hwndEntity);
  561.         }
  562.     
  563.         if (update_bits & (W_XY | W_XY_OVERLAY))
  564.         {
  565.             InvalidateRect(g_qeglobals.d_hwndXY, NULL, false);
  566.             UpdateWindow (g_qeglobals.d_hwndXY);
  567.         }
  568.  
  569.         update_bits = 0;
  570.  
  571.         if (!cambuttonstate && !have_quit)
  572.         {    // if not driving in the camera view, block
  573.             WaitMessage ();
  574.         }
  575.  
  576.     }
  577.  
  578.     /* return success of application */
  579.     return TRUE;
  580.  
  581. }
  582.  
  583.